package com.example.scity.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.example.scity.APIInterface;
import com.example.scity.Configs;
import com.example.scity.NewsInfoActivity;
import com.example.scity.R;
import com.example.scity.beens.NewsBeen;
import com.example.scity.beens.NewsTypeBeen;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class Fragment4 extends Fragment {
    View view;
    NewsBeen newsBeen;
    NewsBeen newsBeen2;
    NewsTypeBeen newsTypeBeen;
    RadioGroup radioGroup;
    ListView listView;

    Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            switch (msg.what){
                case 1:
                    DisplayHotNews();
                    break;
                case 2:
                    DisplayNewsType();//分类列表
                    break;
                case 3:
                    DisplayNewsList();//列表详情
                    break;
            }
        }
    };

    private void DisplayNewsList() {
        listView.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {//提供been数据总量，告诉listview总共有多少条新闻
                return newsBeen2.rows.size();
            }

            @Override
            public Object getItem(int position) {
                return newsBeen2.rows.get(position);
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            //对每一条新闻表现方式的实现
            public View getView(int position, View convertView, ViewGroup parent) {
                //当前这条新闻的样式是R.layout.item_news
                View v = LayoutInflater.from(getActivity()).inflate(R.layout.item_news,null);
                //接下来为每个数据项做显示
                //首先通过Glide显示照片把newsBeen2里面的cover地址的照片显示在imageview
                ImageView imageView = v.findViewById(R.id.imageView3);
                String url = Configs.Host+newsBeen2.rows.get(position).cover;
                Glide.with(getActivity()).load(url).into(imageView);
                //2、把newsBeen2的第position条数据的标题显示在textview上
                TextView textView1 = v.findViewById(R.id.newtitle);
                textView1.setText(newsBeen2.rows.get(position).title);
                //3、显示新闻简介
                TextView textView2 = v.findViewById(R.id.newscontent);
                textView2.setText(Html.fromHtml(newsBeen2.rows.get(position).content));
                //4、显示时间
                TextView textView3 = v.findViewById(R.id.newstime);
                textView3.setText("时间:"+newsBeen2.rows.get(position).publishDate);
                //5、评论数
                TextView textView4 = v.findViewById(R.id.newpijia);
                textView4.setText("评论："+newsBeen2.rows.get(position).commentNum+"");
                return v;
            }
        });
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = newsBeen2.rows.size()*160;
        listView.setLayoutParams(params);
    }

    private void DisplayNewsType() {
        for(int i = 0 ;i<newsTypeBeen.data.size();i++){
            switch (i){
                case 0:
                    ((RadioButton)view.findViewById(R.id.radioButton)).setText(newsTypeBeen.data.get(i).name);
                    break;
                case 1:
                    ((RadioButton)view.findViewById(R.id.radioButton6)).setText(newsTypeBeen.data.get(i).name);
                    break;
                case 2:
                    ((RadioButton)view.findViewById(R.id.radioButton7)).setText(newsTypeBeen.data.get(i).name);
                    break;
                case 3:
                    ((RadioButton)view.findViewById(R.id.radioButton8)).setText(newsTypeBeen.data.get(i).name);
                    break;
                case 4:
                    ((RadioButton)view.findViewById(R.id.radioButton9)).setText(newsTypeBeen.data.get(i).name);
                    break;
                case 5:
                    ((RadioButton)view.findViewById(R.id.radioButton10)).setText(newsTypeBeen.data.get(i).name);
                    break;
                default:
                    return;
            }
        }
        radioGroup.check(R.id.radioButton);
    }
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_4,container,false);

        InitListView();

        InitLinearLayout();

        LoadHotNews();//加载专题新闻

        LoadNesTypes();//加载新闻类别

        InitRadioGroup();//初始化radiogroup



        return view;
    }

    private void InitLinearLayout() {
        LinearLayout linearLayout = view.findViewById(R.id.zhuanti1);
        LinearLayout linearLayout1 = view.findViewById(R.id.zhuangti2);
        linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), NewsInfoActivity.class);
                intent.putExtra("ID",newsBeen.rows.get(0).id);
                startActivity(intent);
            }
        });
        linearLayout1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), NewsInfoActivity.class);
                intent.putExtra("ID",newsBeen.rows.get(1).id);
                startActivity(intent);
            }
        });
    }

    private void InitListView() {
        listView = view.findViewById(R.id.newslist);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), NewsInfoActivity.class);
                intent.putExtra("ID",newsBeen2.rows.get(position).id);
                startActivity(intent);
            }
        });
    }

    private void LoadNews(int type) {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(Configs.Host+ APIInterface.NewsList + "?type="+type).build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {

            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                newsBeen2 = new Gson().fromJson(response.body().string(), NewsBeen.class);
                handler.sendEmptyMessage(3);//加载成功
            }
        });
    }

    private void InitRadioGroup() {
        radioGroup = view.findViewById(R.id.xwradiogroup);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(newsTypeBeen==null)return;
                switch (checkedId){
                    case R.id.radioButton:
                        if(newsTypeBeen.data.size()>=1){
                            LoadNews(newsTypeBeen.data.get(0).id);
                        }
                        break;
                    case R.id.radioButton6:
                        if(newsTypeBeen.data.size()>=2){
                            LoadNews(newsTypeBeen.data.get(1).id);
                        }
                        break;
                    case R.id.radioButton7:
                        if(newsTypeBeen.data.size()>=3){
                            LoadNews(newsTypeBeen.data.get(2).id);
                        }
                        break;
                    case R.id.radioButton8:
                        if(newsTypeBeen.data.size()>=4){
                            LoadNews(newsTypeBeen.data.get(3).id);
                        }
                        break;
                    case R.id.radioButton9:
                        if(newsTypeBeen.data.size()>=5){
                            LoadNews(newsTypeBeen.data.get(4).id);
                        }
                        break;
                    case R.id.radioButton10:
                        if(newsTypeBeen.data.size()>=6){
                            LoadNews(newsTypeBeen.data.get(5).id);
                        }
                        break;
                }
            }
        });
        //radioGroup.check(R.id.radioButton);
    }

    private void LoadNesTypes() {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(Configs.Host+ APIInterface.NewsTypeList).build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {

            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                newsTypeBeen = new Gson().fromJson(response.body().string(), NewsTypeBeen.class);
                handler.sendEmptyMessage(2);//加载成功
            }
        });
    }

    private void DisplayHotNews() {
        ImageView imageView1;
        ImageView imageView2;
        TextView textView1;
        TextView textView2;
        imageView1 = view.findViewById(R.id.imageView);
        imageView2 = view.findViewById(R.id.imageView2);
        textView1 = view.findViewById(R.id.textView4);
        textView2 = view.findViewById(R.id.textView5);
        Glide.with(getActivity()).load(Configs.Host+newsBeen.rows.get(0).cover).into(imageView1);
        Glide.with(getActivity()).load(Configs.Host+newsBeen.rows.get(1).cover).into(imageView2);
        textView1.setText(Html.fromHtml(newsBeen.rows.get(0).content));
        textView2.setText(Html.fromHtml(newsBeen.rows.get(1).content));

    }

    private void LoadHotNews() {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(Configs.Host+ APIInterface.NewsList + "?hot=Y").build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {

            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                newsBeen = new Gson().fromJson(response.body().string(), NewsBeen.class);
                handler.sendEmptyMessage(1);//加载成功
            }
        });
    }
}
